package com.kingdee.eas.custom.buscost.handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.IMetaDataLoader;
import com.kingdee.bos.metadata.MetaDataLoaderFactory;
import com.kingdee.bos.metadata.entity.EntityObjectInfo;
import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.web.handler.ListHandler;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;

/**
 * 
 * Title: AgainstApprovalHandler
 * Description: 反审批
 * @author saisai_cheng Email:854296216@qq.com
 * @date 2019-7-31
 */
public class AgainstApprovalHandler extends ListHandler {

	private static Logger logger = Logger
			.getLogger("com.kingdee.eas.custom.buscost.handler.AgainstApprovalHandler");

	private Context ctx;

	public AgainstApprovalHandler(Context ctx) {
		this.ctx = ctx;
	}

	public AgainstApprovalHandler() {
		this.ctx = SHRContext.getInstance().getContext();
	}

	/**
	 * 
	 * <p>
	 * Title: againstApproveAction
	 * </p>
	 * <p>
	 * Description: 反审批
	 * </p>
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 * @throws ShrWebBizException
	 */
	public String againstApproveAction(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap)
			throws ShrWebBizException {
		String billId = request.getParameter("billId");
		String tableName = getTableNameByBillId(billId);
		// 反审批单据
		String msg = againstBillState(tableName, billId);


		modelMap.put("flag", msg);
		try {
			JSONUtils.writeJson(response, modelMap);
		} catch (SHRWebException e) {
			e.printStackTrace();
			throw new ShrWebBizException("反审批失败！" + e);
		}

		return null;

	}

	private String againstBillState(String tableName, String fid)
			throws ShrWebBizException {
		if (StringUtils.isEmpty(tableName) || StringUtils.isEmpty(fid)) {
			throw new ShrWebBizException(" 反审批失败！");
		}
		String msg = "";
		String sql = " update " + tableName
				+ " set fbillstate = '0'  where fid = '" + fid + "'";
		try {
			DbUtil.execute(ctx, sql);
			msg = "1";
		} catch (BOSException e) {
			e.printStackTrace();
			logger.error("设置单据状态 为 未提交 失败！" + e);
		}

		return msg;

	}

	

	private String getTableNameByBillId(String billId) {
		if (StringUtils.isNotEmpty(billId)) {
			BOSUuid uuid = BOSUuid.read(billId);
			BOSObjectType bosType = uuid.getType();
			IMetaDataLoader metadataloader = MetaDataLoaderFactory
					.getRemoteMetaDataLoader();
			EntityObjectInfo entity = metadataloader.getEntity(bosType);
			String tableName = entity.getTable().getName();

			return tableName;
		}

		return null;

	}
}
